package com.example.administrator.szb.fragments.fragment_forTab.home.presenter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.example.administrator.szb.activity.base.BasePresenter;
import com.example.administrator.szb.bean.IndexBean;
import com.example.administrator.szb.bean.TmpZxBean;
import com.example.administrator.szb.bean.VersionBean;
import com.example.administrator.szb.tinkerutil.SampleApplicationLike;
import com.example.administrator.szb.fragments.fragment_forTab.home.bean.LuYanList;
import com.example.administrator.szb.fragments.fragment_forTab.home.bean.SeriveList;
import com.example.administrator.szb.fragments.fragment_forTab.home.bean.Supeis;
import com.example.administrator.szb.fragments.fragment_forTab.home.model.HomeModelImpl;
import com.example.administrator.szb.fragments.fragment_forTab.home.view.HomeView;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.HttpUtils;
import com.example.administrator.szb.http.OnResultListener;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.AppUtils;
import com.example.administrator.szb.util.FinishActivity;
import com.example.administrator.szb.util.SharedPreferencesUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by stone on 2018/1/19.
 */

public class HomePresenter extends BasePresenter<HomeView> implements OnResultListener {
    HomeView homeView;
    HomeModelImpl homeModel;
    public HomePresenter(HomeView homeView) {
        this.homeView = homeView;
        homeModel = new HomeModelImpl();
    }

    public void getFromNet(){
        homeView.hideErrorNet();
        homeModel.getData(activity,this);
        getZx();
        getLuyan();
        getService();
        getSuPei();
        quitAll();
    }
    public void getLocal(){
        homeView.onSuccess(SampleApplicationLike.getIndexBeanInstance(),true);
    }
    @Override
    public void onSuccess(int requestCode, String str) {
        IndexBean indexBean = JSON.parseObject(str,new TypeReference<IndexBean>(){});
        SampleApplicationLike.setIndexBeanInstance(indexBean);
        SharedPreferencesUtil.savaData(SampleApplicationLike.getInstance(),indexBean,"index");
        homeView.onSuccess(indexBean,false);
    }

    @Override
    public void onFailure(int requestCode, String str) {
        homeView.onFail(str);
        if (SampleApplicationLike.getIndexBeanInstance()==null){
            homeView.hideContent();
            homeView.showErrorNet();

        }
    }

    public void getLuyan(){
        String url = URLAddress.LUYAN_GETALL;
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("page_count",3);
        HttpUtils.post(activity,url,map ,new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {

                homeView.showLuyan(JSON.parseArray(str, LuYanList.class));
            }

            @Override
            public void onFailure(int requestCode, String str) {

            }
        });
    }


    public void getService(){
        String url = URLAddress.SPECIAL_GETALL;
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("page_count",3);
        HttpUtils.post(activity,url,map ,new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {

                homeView.showService(JSON.parseArray(str, SeriveList.class));
            }

            @Override
            public void onFailure(int requestCode, String str) {

            }
        });
    }
    public void getSuPei(){
        String url = URLAddress.SUPEI_LIST;
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("class",1);
        HttpUtils.post(activity,url,map ,new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {
                str = str.replace("\"1\":","\"first\":");
                str = str.replace("\"0\":[]","\"0\":{}");
                str = str.replace("\"0\":","\"zero\":");
                homeView.showSuPei(JSON.parseArray(str, Supeis.class));
            }

            @Override
            public void onFailure(int requestCode, String str) {

            }
        });
    }

    public void getZx(){
        String url = "http://119.29.63.230/24h/news.json?newsid=0";
        HttpUtils.get2(activity,url, 0, new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {

                homeView.showZX(JSON.parseObject(str,new TypeReference<TmpZxBean>(){}));
            }

            @Override
            public void onFailure(int requestCode, String str) {

            }
        });
    }
    private void quitAll(){
        Map<String,Object> params = new HashMap<>();
        params.put("code", HttpUtil.getParam());
        params.put("version", AppUtils.getVersionCode(activity));
        params.put("app", "android");
        HttpUtils.post(activity,URLAddress.QUIT_ALL, params, new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {
                VersionBean versionBean = JSON.parseObject(str,new TypeReference<VersionBean>(){});
                if (versionBean.getIscompellent()==0){
                    FinishActivity.finish();
                }
            }

            @Override
            public void onFailure(int requestCode, String str) {

            }
        });
    }
}
